function Calculator(doc, options) {
  // 需要操作的 dom 节点获取到
  this.dom = doc.querySelector(options.dom);
  this.val1 = this.dom.querySelectorAll('input')[0];
  this.val2 = this.dom.querySelectorAll('input')[1];
  this.result = this.dom.querySelector('.result');

  this.init();

  // 检查
  // console.log(this.dom);
  // console.log(this.val1);
  // console.log(this.val2);
  // console.log(this.result);
}

Calculator.prototype.init = function () {
  this.bindEvent();
}

Calculator.prototype.bindEvent = function () {
  this.dom.addEventListener('click', this.handleEvent.bind(this), false);
}

Calculator.prototype.handleEvent = function (event) {
  // 兼容性
  var e = event || window.event;
  var target = e.target;
  var tagName = target.tagName.toLowerCase();
  var result;

  if (tagName === 'button') {
    result = this.calculate(target.dataset.type);
    // console.log(result);
    this.result.innerHTML = result;
  }
}

Calculator.prototype.calculate = function (type) {
  var val1 = Number(this.val1.value) || 0;
  var val2 = Number(this.val2.value) || 0;

  switch (type) {
    case 'plus':
      return val1 + val2;
      break;
    case 'minus':
      return val1 - val2;
      break;
    case 'mul':
      return val1 * val2;
      break;
    case 'dir':
      return val1 / val2;
      break;
    default:
      break;
  }
}


new Calculator(document, {
  dom: '.j-calculator1'
});

new Calculator(document, {
  dom: '.j-calculator2'
});